﻿using Common;
using DataLibrary;
using System;
using System.Linq;
using System.Windows.Forms;

namespace TicketSystem
{
    public partial class Form_TravelTypeAdd : Form
    {
        public Form_TravelTypeAdd()
        {
            InitializeComponent();
        }
        private TicketSystemEntities tse = new TicketSystemEntities();
        private void btn_Save_Click(object sender, EventArgs e)
        {
            var count = tse.DepartmentTypes.Where(u => u.TypeName == this.txtTypeName.Text).Count();
            if (count == 0)
            {
                DepartmentTypes departmenttype = new DepartmentTypes();
                departmenttype.TypeName = this.txtTypeName.Text;
                departmenttype.Created_at = System.DateTime.Now;
                departmenttype.Updated_at = System.DateTime.Now;
                departmenttype.OperatorID = GetOperator.OperatorID;
                //departmenttype.OperatorID = 1;
                tse.Configuration.ValidateOnSaveEnabled = false;
                tse.DepartmentTypes.Add(departmenttype);
                if (tse.SaveChanges() >= 1)
                {
                    MessageBox.Show("添加成功");
                    this.Close();
                }
                else
                {
                    MessageBox.Show("添加失败！");
                }
            }
            else
            {
                MessageBox.Show("你要添加的类型已存在");
            }
        }

        private void btn_Next_Click(object sender, EventArgs e)
        {
            var count = tse.DepartmentTypes.Where(u => u.TypeName == this.txtTypeName.Text).Count();
            if (count == 0)
            {
                DepartmentTypes departmenttype = new DepartmentTypes();
                departmenttype.TypeName = this.txtTypeName.Text;
                departmenttype.Created_at = System.DateTime.Now;
                departmenttype.Updated_at = System.DateTime.Now;
                departmenttype.OperatorID = GetOperator.OperatorID;
                tse.Configuration.ValidateOnSaveEnabled = false;
                tse.DepartmentTypes.Add(departmenttype);
                if (tse.SaveChanges() >= 1)
                {
                    MessageBox.Show("添加成功");
                    this.txtTypeName.Text = null;
                }
                else
                {
                    MessageBox.Show("添加失败！");
                }
            }
            else
            {
                MessageBox.Show("你要添加的类型已存在");
            }
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
